<?php


namespace App\Http\Controllers\Admin\Interaction;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

//房间互动变声效果管理
class InteractionChangeSoundController extends BaseController
{
    /**
     * @api {get} /interaction/changesound/list 变声效果列表
     * @apiVersion 0.1.0
     * @apiName 变声效果列表
     * @apiGroup 房间互动变声效果管理
     *
     * @apiParam {Int}                     page 页码（选填）
     * @apiParam {Int}                       per_page 一页行数 （选填）
     *
     * @apiSuccessExample 成功响应
     *
     * {
     * "code": 200,
     * "data": {
     * "total": 1,
     * "current_page": 1,
     * "last_page": 1,
     * "list": [
     * {
     * "id": 1,
     * "name": "大笑",
     * "status": 1,
     * "icon": "URL",
     * "sort": 100,
     * "add_time": 2021-07-16 15:03:52,
     * "update_time": null
     * }
     * ]
     * }
     * }
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function list(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'page' => 'filled|integer|gt:0',
                'per_page' => 'filled|integer|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $field = ['id', 'change_name as name', 'status', 'icon', 'sort','add_time','update_time'];
        $list = app('model_room_interaction_change_sound')
            ->orderBy('sort', 'desc')
            ->paginate($perPage, $field, '', $page)->toArray();
        //返回值有固定格式
        $result = [
            'total' => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];
        return Response::sendData($result);
    }

    /**
     * @api {post} /interaction/changesound/delete 批量删除（废弃中）
     * @apiVersion 0.1.0
     * @apiName 批量删除变声效果
     * @apiGroup 房间互动变声效果管理
     *
     * @apiParam {String}                     ids  用","分隔（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
//    public function delete(Request $request)
//    {
//        $ids = $request->input('ids', null);
//        $idArray = explode(',', $ids);
//        if (empty($ids)) {
//            throw new BasicException(10001);
//        }
//        $res = app('model_room_interaction_change_sound')->destroy($idArray);
//        if ($res == 0) {
//            throw new BasicException(10001);
//        }
//        return Response::sendData(true);
//    }

    //显示与屏蔽状态的切换

    /**
     * @api {post} /interaction/changesound/changestatus 改变变声效果显示状态
     * @apiVersion 0.1.0
     * @apiName 改变变声效果显示状态
     * @apiGroup 房间互动变声效果管理
     *
     * @apiParam {Int}                     id  （必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function changeStatus(Request $request)
    {
        $id = $request->input('id', null);
        if (empty($id)) {
            throw new BasicException(10001);
        }
        $model = app('model_room_interaction_change_sound');
        $detail = $model->where('id', $id)->first();
        if (empty($detail)) {
            throw new BasicException(10001);
        }
        if ($detail['status'] == 0) {
            $res = $model->where('id', $id)->update(['status' => 1, 'update_time' => time()]);
        } else {
            $res = $model->where('id', $id)->update(['status' => 0, 'update_time' => time()]);
        }

        if ($res) {
            return Response::sendData(true);
        } else {
            throw new BasicException(10001);
        }
    }


    /**
     * @api {post} /interaction/changesound/addandup 添加与修改数据
     * @apiVersion 0.1.0
     * @apiName 添加与修改数据
     * @apiGroup 房间互动变声效果管理
     *
     * @apiParam {Int}     id          有id表示修改（选填）
     * @apiParam {String}  name 变声器音效名称（必填）
     * @apiParam {String}  icon        图标地址（必填）
     * @apiParam {Int}     status      状态值 0屏蔽 1显示 （必填）
     * @apiParam {Int}     sort        排序值（必填）
     *
     * @apiSuccessExample 成功响应
     *          {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function addAndUp(Request $request)
    {
        $data = $request->all();

        $va = app('validator')->make($data, [
            'name' => 'required|string|max:64',
            'icon' => 'required|string|max:255',
            'status' => 'required|in:0,1',
            'sort' => 'required|integer|min:0'
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $insertData = [
            'change_name' => $data['name'],
            'icon' => $data['icon'],
            'status' => $data['status'],
            'sort' => $data['sort']
        ];

        if (isset($data['id'])) {
            $insertData['update_time'] = time();
            $res = app('model_room_interaction_change_sound')
                ->where('id', $data['id'])
                ->update($insertData);
        } else {
            $insertData['add_time'] = time();
            $res = app('model_room_interaction_change_sound')
                ->insert($insertData);
        }
        if ($res) {
            return Response::sendData(true);
        } else {
            throw new BasicException(10001);
        }

    }
}